package com.lyc.user.serlvet;

import com.lyc.user.bean.User;
import com.lyc.user.servceImpl.UserServiceImpl;
import com.lyc.user.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

@WebServlet("/userRegisterServlet")
public class UserRegister extends HttpServlet {
    private UserService userService=new UserServiceImpl();
    private User user=new User();
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      req.setCharacterEncoding("UTF-8");
      resp.setCharacterEncoding("UTF-8");
      resp.setContentType("text/html");

        String methodName = req.getParameter("methodName");
        try {
            Method  method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.setAccessible(true);
            method.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      doGet(req, resp);
    }
    //    电话注册未完成
    public void userPhoneRegister(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String phone = req.getParameter("phone");
        String veriCode = req.getParameter("veriCode");

        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");

         user.setPhone(phone);
         user.setVeriCode(veriCode);

        String result = userService.phoneRegister(user);
        resp.getWriter().print(result);
    }
    //用户账号注册
    public void userAccountRegister(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String account=req.getParameter("account");
        String pwd1 = req.getParameter("pwd1");

        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        user.setAccount(account);
        user.setPwd(pwd1);

        String result = userService.userAccountRegister(user);
        resp.getWriter().print(result);
//        System.out.println(result);
    }
}
